package Controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;

import javax.swing.JComboBox;

import Entity.Customer;
import DB.CustomerQuery;
import Communication.Client;
import Communication.MessageHandle;
import View.UpdateCustomerDeatilsView;
import View.UpdateCustomerView;


/** This class controls and manages the process that allows to update customer info */
public class UpdateCustomerController implements Serializable {
	private static final long serialVersionUID = 42L;
	/**  Required attributes */
	CustomerQuery customerModel;
	static UpdateCustomerView customerView;
	static UpdateCustomerDeatilsView updateCustomerDeatilsView;
	CustomerInfoListener customerInfoListener;
	Client client;
	UpdateCustomerDetailsController customerDetailController;



	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public UpdateCustomerController(Client client, WorkerMainMenuController workerMainMenuController) {
			
		this.client = client;
		//customerModel = new CustomerQuery(client);
		customerView =  new UpdateCustomerView();
		customerView.showWindow();
		customerInfoListener = new CustomerInfoListener(workerMainMenuController);
		updateCustomerDeatilsView = new UpdateCustomerDeatilsView();
		updateCustomerDeatilsView.setLocation(450, 200);
		customerView.updateCustomerListener(customerInfoListener );
		customerView.setLocation(450, 200);
		updateCustomerDeatilsView .addActionListenerOur(customerInfoListener);
	}
	
	/**  Inner Listener class */ 
	class CustomerInfoListener implements ActionListener
	{
		WorkerMainMenuController workerMainController;
		
		/**
		 * creats CustomerInfo Listener
		 * @param workerMainController
		 */
		public CustomerInfoListener(WorkerMainMenuController workerMainController){
			this.workerMainController = workerMainController;
			customerModel = new CustomerQuery();
			customerView.showWindow();
			workerMainController.hideWindow();
		}


		@Override
		/**
		 * Handle Actions
		 */
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == customerView.getSearchBtn() || event.getActionCommand().compareTo("Update")==0 ) {
				if (customerModel.isEmpty(customerView.getID()))
					SystemController
							.displayErrorMessage("ID Field Is Missing!");
				else {
					Customer cust = new Customer();
					switch (event.getActionCommand()) {
					case "Search":			//search button pressed button
						cust.setCustomerID(customerView.getID());
						MessageHandle msg = new MessageHandle(cust,
								MessageHandle.UPDATE_GET_CUSTOMER, false);
						msg.setMsg("FindCustomer");
						try {
							client.sendToServer(msg);
						} catch (IOException e) {
							System.out
									.println("UpdateCustomerContriller->ActionPerformed");
							e.printStackTrace();
						}
						
						break;
					case "Update":			//Update button pressed button
						int confirm=SystemController.displayConfirmationMessage("Are You Sure?");
						if(confirm ==0){
							cust.setCfirstName(updateCustomerDeatilsView.getFirstName());
							cust.setClastName(updateCustomerDeatilsView.getlastName());
							cust.setCity(updateCustomerDeatilsView.getCity());
							cust.setPhoneNumber(updateCustomerDeatilsView.getPhone());
							cust.setCustomerID(updateCustomerDeatilsView.getID());
							MessageHandle msg2 = new MessageHandle(cust,
									MessageHandle.UPDATE_GET_CUSTOMER, false);
							msg2.setMsg("UpdateCustomer");
							try {
								client.sendToServer(msg2);
							} catch (IOException e) {
								System.out
										.println("UpdateCustomerContriller->ActionPerformed->updatecase");
								e.printStackTrace();
							}
						}
						break;
						
					case "Back":			//Update button pressed button
//						workerMainController.showWindow();
						customerView.hideWindow();
						customerView.clearFields();
						break;
					}
				}
			}else{
				if (event.getSource() == customerView.getBackBtn())
				{	customerView.hideWindow();
				workerMainController.showWindow();
				}
				else {
					// workerMainController.showWindow();
					updateCustomerDeatilsView.hideWindow();
					customerView.showWindow();
					customerView.clearFields();
				}
			}
		}
	}

	/**
	 *  shows the window
	 */
	public void showWindow() {
			customerView.showWindow();		
		}
	
	
	@SuppressWarnings("deprecation")
	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void UpdateView(MessageHandle msg) {
		switch (msg.getMsg()) {
		case "NotExist":
			SystemController.displayErrorMessage("Customer do not exist!");
			break;
		case "Exist":
			customerView.hideWindow();
			updateCustomerDeatilsView.show();
			updateCustomerDeatilsView.setDetails(msg.getData());
			break;
		case "UpdateSuccess":
			Customer cust = (Customer) msg.getData();
			SystemController.displayMessage("Customer "+ cust.getFirstName() +" "+ cust.getLastName() +" Details \n have been Updated!!");
		}
	}
}





